#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*
 * force type conversion of struct.
 */

struct blank
{

};

typedef struct aaa
{
	unsigned char a1;
	unsigned char a2;
	char a3[6];
	char a4[8];
}aaa;

int main()
{
	char buf[30];
	
	memset(buf, '0', sizeof(buf));
	strcpy(buf,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
	
	printf("buf length : %d\n", sizeof(buf));
        printf("struct length : %d\n", sizeof(struct blank));
	printf("struct length : %d\n", sizeof(struct aaa));
	
	aaa *aa;
	aa = (struct aaa*)buf;
	
	// 按照结构里的类型填充各个字段.
	printf("a1 is %d\n", aa->a1);
	printf("a1 is %c\n", aa->a1);
	//printf("a1 is %s\n", aa->a1);	Segmentation fault. 因为字段是单个字节.	2012/11/1
	
	printf("a2 is %d\n", aa->a2);
	printf("a2 is %c\n", aa->a2);
	//printf("a2 is %s\n", aa->a2);	Segmentation fault	因为字段是单个字节.

	printf("a3 is %s\n", aa->a3);		// why not overflow, xiaozhi? 因为被转换成了指针
	printf("a4 is %s\n", aa->a4);		// ditto

	printf("a3 is %.6s\n", aa->a3);
	printf("a4 is %.8s\n", aa->a4);
	
	return 0;
}



